package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.ArrayList;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.system.mapper.SysRoleMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysDirtreeMapper;
import com.ruoyi.system.domain.SysDirtree;
import com.ruoyi.system.service.ISysDirtreeService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 目录树Service业务层处理
 * 
 * @author ruoyi
 * @date 2019-11-11
 */
@Service
public class SysDirtreeServiceImpl implements ISysDirtreeService 
{
    @Autowired
    private SysDirtreeMapper sysDirtreeMapper;

    @Autowired
    private SysRoleMapper sysRoleMapper;

    /**
     * 查询目录树
     * 
     * @param drrId 目录树ID
     * @return 目录树
     */
    @Override
    public SysDirtree selectSysDirtreeById(Long drrId)
    {
        return sysDirtreeMapper.selectSysDirtreeById(drrId);
    }

    /**
     * 查询目录树列表
     * 
     * @param sysDirtree 目录树
     * @return 目录树
     */
    @Override
    public List<SysDirtree> selectSysDirtreeList(SysDirtree sysDirtree)
    {
        return sysDirtreeMapper.selectSysDirtreeList(sysDirtree);
    }

    /**
     * 新增目录树
     * 
     * @param sysDirtree 目录树
     * @return 结果
     */
    @Override
    public int insertSysDirtree(SysDirtree sysDirtree)
    {
        return sysDirtreeMapper.insertSysDirtree(sysDirtree);
    }

    /**
     * 修改目录树
     * 
     * @param sysDirtree 目录树
     * @return 结果
     */
    @Override
    @Transactional
    public int updateSysDirtree(SysDirtree sysDirtree)
    {
        SysDirtree dirtree = sysDirtreeMapper.selectSysDirtreeById(sysDirtree.getDrrId());
        if(!sysDirtree.getStatus().equals(dirtree.getStatus())){
            List<Long> list = sysDirtreeMapper.selectChildIDsByID(sysDirtree.getDrrId());
            int i = sysRoleMapper.updateStatusBydirtreeids(list, sysDirtree.getStatus());
            if(i==0){
                throw new BusinessException("更新角色状态失败");
            }
            list.remove(sysDirtree.getDrrId());
            int i1 = sysDirtreeMapper.updateStatusByIDS(list, sysDirtree.getStatus());
            if(i1==0){
                throw new BusinessException("更新子目录状态失败");
            }
        }
        return sysDirtreeMapper.updateSysDirtree(sysDirtree);
    }

    /**
     * 删除目录树对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteSysDirtreeByIds(String ids)
    {
        return sysDirtreeMapper.deleteSysDirtreeByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除目录树信息
     * 
     * @param drrId 目录树ID
     * @return 结果
     */
    @Override
    public int deleteSysDirtreeById(Long drrId)
    {
        if(sysDirtreeMapper.selectFirstChildCountById(drrId)>0){
            throw new BusinessException("存在子目录，不能删除");
        }
        if(sysDirtreeMapper.selectFirstRoleCountById(drrId)>0){
            throw new BusinessException("目录下存在角色，不能删除");
        }
        return sysDirtreeMapper.deleteSysDirtreeById(drrId);
    }

    /**
     * 查询目录树树列表
     * 
     * @return 所有目录树信息
     */
    @Override
    public List<Ztree> selectSysDirtreeTree(SysDirtree tree)
    {
        List<SysDirtree> sysDirtreeList = sysDirtreeMapper.selectSysDirtreeList(tree);
        List<Ztree> ztrees = new ArrayList<Ztree>();
        for (SysDirtree sysDirtree : sysDirtreeList)
        {
            Ztree ztree = new Ztree();
            ztree.setId(sysDirtree.getDrrId());
            ztree.setpId(sysDirtree.getParentId());
            ztree.setName(sysDirtree.getName());
            ztree.setTitle(sysDirtree.getName());
            ztrees.add(ztree);
        }
        return ztrees;
    }
}
